package com.ruoyi.system.keyStrategy;

import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 键更换策略
 *
 * @author xuezhenwei
 * @date 2024-06-17 9:49
 */
@Component
public class KeyUtils {

    @Autowired
    private RedisTemplate redisTemplate;

    private String TAG = "lcqxyj";

    private String weather = "weather:";

    private String SALE = "sale:";

    @Value("${key.count}")
    private Integer value = 900;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    private List<String> KEYS = Stream.of(
                    "6fc0bdccce624bb6a68bc59aa242ffcd",
                    "1884dba16bee49b2b137da1130cb7422",
                    "08145dba9b6f451f926c7f548e1314cc",
                    "59c6f9e90e144679b488cab7d5badf7d",
                    "578101cab52b4e3f9cd93d5990092556",
                    "c60f43684dc94e30a26bd62b6feb12af",
                    "cc977749066e45cca107881c1fbaf061")
            .collect(Collectors.toList());


    public String get() {

        Iterator<String> iterator = KEYS.iterator();

        while (iterator.hasNext()) {
            String key = iterator.next();
            String redisKey = TAG + ":" + getTime() + ":" + key;
            Object object = redisTemplate.opsForValue().get(redisKey);

            // 如果没找到缓存证明今天还没创建缓存
            if (Objects.isNull(object)) {
                SpringUtil.getBean(RedisCache.class).setCacheObject(redisKey, 1);
//                redisTemplate.opsForValue().increment(redisKey , 1);
                return key;
            }

            Integer value = (Integer) object;
            if (value <= this.value) {
                redisTemplate.opsForValue().increment(redisKey, 1);
                return key;
            }

        }
        return "";

    }

    private String getTime() {
        return simpleDateFormat.format(new Date());
    }

    public String getWeatherInfoByKey(String key) {
        String result = "";
        if (StringUtils.isBlank(key))
            return result;
        Object obj = redisTemplate.opsForValue().get(weather + key);

        if (Objects.isNull(obj))
            return result;

        return String.valueOf(obj);

    }

    public String getSaleInfoByKey(String key) {
        String result = "";
        if (StringUtils.isBlank(key))
            return result;
        Object obj = redisTemplate.opsForValue().get(SALE + key);

        if (Objects.isNull(obj))
            return result;

        return String.valueOf(obj);

    }

    public void setWeatherInfoByKey(String key, String value) {
        this.setWeatherInfoByKey(key, value, 4);
    }

    public void setWeatherInfoByKey(String key, String value, long time) {

        if (StringUtils.isBlank(key))
            return;
        try {
            redisTemplate.opsForValue().set(weather + key, value, time, TimeUnit.MINUTES);
        } catch (Exception e) {
            System.out.println("保存实时天气缓存失败,key=" + key);
        }
    }

    public void setSaleInfoByKey(String key, String value) {
        this.setSaleInfoByKey(key, value, 120);
    }

    public void setSaleInfoByKey(String key, String value, long time) {

        if (StringUtils.isBlank(key))
            return;
        try {
            redisTemplate.opsForValue().set(SALE + key, value, time, TimeUnit.MINUTES);
        } catch (Exception e) {
            System.out.println("保存实时天气缓存失败,key=" + key);
        }
    }


    public Map<String, Object> getValueList() {
        Collection<String> keys = SpringUtils.getBean(RedisCache.class).keys(TAG + ":" + getTime() + ":" + "*");
        if (CollectionUtils.isEmpty(keys)) {
            return null;
        }

        HashMap<String, Object> map = new HashMap<>();

        keys.forEach(key -> {
            Object obj = redisTemplate.opsForValue().get(key);
            map.put(key, obj);
        });
        return map;
    }

    public int setValueByKey(String key, Long value) {
        try {
            SpringUtil.getBean(RedisCache.class).setCacheObject(key, value);
        } catch (Exception e) {
            return 1;
        }
        return 0;
    }

}
